package com.say.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.say.bean.common.Response;
import com.say.bean.common.SplitPage;
import com.say.bean.domain.Share;
import com.say.bean.domain.Trace;
import com.say.bean.domain.User;
import com.say.bean.domain.Words;
import com.say.service.FriendsService;
import com.say.service.ShareService;
import com.say.service.TraceService;
import com.say.service.UserService;
import com.say.service.WordsService;
import com.say.utils.DateHelper;
import com.say.utils.UserUtil;

@Controller
@RequestMapping("/wensay/share")
public class ApiShareController extends BaseController {

	 @Autowired
	 public ShareService shareService;
	 
	 @Autowired
	 public FriendsService friendsService;
	 
	 @Autowired
	 public UserService userService;
	 
	 @Autowired
	 public WordsService wordsService;
	 
	 @Autowired
	 public TraceService traceService;
	 
	//发现页面
	@RequestMapping()
	public String share(Model model){
		User user = UserUtil.getUser(request);
		Words words=wordsService.getRandomByWordsType(1);
		List<User> users=userService.findFriendsByUserId(user.getId());
		users.add(user);
		model.addAttribute("users", users);
		model.addAttribute("user", user);
		model.addAttribute("words", words);
		traceService.save(new Trace(user.getId(),"0.0.0.0","访问日记页面"));
		return "wensay/share";
	}
	
	
	
	@RequestMapping("/issue")
	public String issue(Model model,String imgPath){
		User user = UserUtil.getUser(request);
		model.addAttribute("user", user);
		model.addAttribute("imgPath", StringUtils.isNotEmpty(imgPath) ? imgPath : null);
		return "wensay/issue";
	}
	
	
	
	@RequestMapping("/page")
	@ResponseBody
    public Response page(Model model,int pageSize,int pageNo) {
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		String userId=user.getId();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
       
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String, Object>> page = shareService.findShareByPage(params, initPage(pageNo, pageSize));
        for (Map<String, Object> map : page.getContent()) {
        	map.put("time",DateHelper.format(DateHelper.parseDate(map.get("create_time").toString(), "yyyy-MM-dd HH:mm:ss"),"MM-dd HH:mm"));
        }
        sp.setRowCnt((int) page.getTotalElements());
        response.success(page,"查询成功");
		return response;
    }
	
	
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Response save(Model model,String text,String imgs) {
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		String userId=user.getId();
		
		Share share=new Share();
		share.setUserId(userId);
		if(StringUtils.isNotEmpty(imgs)) {
			String[] img =imgs.split(",");
			if(img.length>=1) {
				share.setImg1(img[0]);
			}
			if(img.length>=2) {
				share.setImg2(img[1]);
			}
			if(img.length>=3) {
				share.setImg3(img[2]);
			}
			if(img.length>=4) {
				share.setImg4(img[3]);
			}
			if(img.length>=5) {
				share.setImg5(img[4]);
			}
			if(img.length>=6) {
				share.setImg6(img[5]);
			}
		}
		
		share.setText(text);
		share.setStatus(1);
		share.setCreateTime(new Date());
		shareService.save(share);
		
		response.success("操作成功");
		return response;
	}
	
	
	@RequestMapping("/del")
	@ResponseBody
    public Response page(Model model,String id) {
		Response response = new  Response();
		Share share=shareService.getById(id);
		if(share!=null) {
			share.setStatus(0);
			shareService.save(share);
		}
		response.success("操作成功");
		return response;
    }
	
}
